/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.enforcement.hid.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.enforcement.common.constant.HidDicConstant;
import com.hopes.bizz.enforcement.hid.dto.HidCheckTaskItemDTO;
import com.hopes.bizz.enforcement.hid.dto.HidChecklistTaskDTO;
import com.hopes.bizz.enforcement.hid.dto.RiskHidCheckTaskDTO;
import com.hopes.bizz.enforcement.hid.entity.RiskHidCheckTaskEntity;
import com.hopes.bizz.enforcement.hid.entity.RiskHidCheckTaskRecordEntity;
import com.hopes.bizz.enforcement.hid.service.RiskHidCheckTaskRecordService;
import com.hopes.bizz.enforcement.hid.service.RiskHidCheckTaskService;
import com.hopes.bizz.enforcement.hid.vo.RiskHidCheckTaskVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.idempotent.annotation.Idempotent;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.boss.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 隐患检查任务
 *
 * @author pig
 * @date 2023-07-25 17:24:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskHidCheckTask" )
@Tag(description = "riskHidCheckTask" , name = "隐患检查任务管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskHidCheckTaskController {

    private final RiskHidCheckTaskService riskHidCheckTaskService;
    private final RiskHidCheckTaskRecordService riskHidCheckTaskRecordService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskHidCheckTask 隐患检查任务
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_view')" )
    public R getriskHidCheckTaskPage(Page page, RiskHidCheckTaskEntity riskHidCheckTask) {
        LambdaQueryWrapper<RiskHidCheckTaskEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(riskHidCheckTask.getTaskName()), RiskHidCheckTaskEntity::getTaskName, riskHidCheckTask.getTaskName());
        wrapper.eq(ObjectUtil.isNotNull(riskHidCheckTask.getTaskStatus()), RiskHidCheckTaskEntity::getTaskStatus, riskHidCheckTask.getTaskStatus());
        Page pageInfo = riskHidCheckTaskService.page(page, wrapper);
        return R.ok(pageInfo.convert(task->BeanUtil.copyProperties(task, RiskHidCheckTaskVO.class)));
    }

    /**
     * 通过id查询隐患检查任务
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        RiskHidCheckTaskEntity entity = riskHidCheckTaskService.getById(id);
        RiskHidCheckTaskVO vo = new RiskHidCheckTaskVO();
        BeanUtils.copyProperties(entity, vo);
        return R.ok(vo);
    }

    /**
     * 新增隐患检查任务
     * @param riskHidCheckTask 隐患检查任务
     * @return R
     */
    @Operation(summary = "新增隐患检查任务" , description = "新增隐患检查任务" )
    @SysLog("新增隐患检查任务" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_add')" )
    public R save(@RequestBody RiskHidCheckTaskDTO riskHidCheckTask) {
        riskHidCheckTaskService.saveTask(riskHidCheckTask);
        return R.ok();
    }

    /**
     * 通过id删除隐患检查任务
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除隐患检查任务" , description = "通过id删除隐患检查任务" )
    @SysLog("通过id删除隐患检查任务" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskHidCheckTaskService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param riskHidCheckTask 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_export')" )
    public List<RiskHidCheckTaskEntity> export(RiskHidCheckTaskEntity riskHidCheckTask) {
        return riskHidCheckTaskService.list(Wrappers.query(riskHidCheckTask));
    }

    /**
     * 提交隐患检查任务
     *
     * @return R
     */
    @Operation(summary = "提交隐患检查任务", description = "提交隐患检查任务")
    @PostMapping("/complete")
    @Idempotent(key = "#checklistTaskDTO.id", expireTime = 5, info = "请勿重复查询")
    public R completeHidCheckTak(@RequestBody HidChecklistTaskDTO checklistTaskDTO) {
        return R.ok(riskHidCheckTaskService.completeTask(checklistTaskDTO));
    }


    /**
     * 提交检查人签名
     *
     * @return R
     */
    @Operation(summary = "提交检查人签名", description = "提交检查人签名")
    @PostMapping("/check/sign")
    @Idempotent(key = "#checklistTaskDTO.id", expireTime = 5, info = "请勿重复查询")
    public R hidCheckTaskSign(@RequestBody HidChecklistTaskDTO checklistTaskDTO) {
        return R.ok(riskHidCheckTaskService.hidCheckTaskSign(checklistTaskDTO));
    }

    /**
     * 提交检查项
     *
     * @return R
     */
    @Operation(summary = "提交检查项", description = "提交检查项")
    @PostMapping("/submit/item")
    @Idempotent(key = "#hidCheckTaskItem.id", expireTime = 5, info = "请勿重复查询")
    public R submitCheckTaskItem(@RequestBody HidCheckTaskItemDTO hidCheckTaskItem) {
        return R.ok(riskHidCheckTaskService.submitCheckTaskItem(hidCheckTaskItem));
    }

    /**
     * 批量提交检查项
     */
    @Operation(summary = "批量提交检查项" , description = "批量提交检查项" )
    @PostMapping("/batch/submit/item")
    @Idempotent(expireTime = 5, info = "请勿重复提交")
    public R batchSubmitCheckTaskItem(@RequestBody Long[] ids) {
        return R.ok(riskHidCheckTaskService.batchSubmitCheckTaskItem(Arrays.asList(ids)));
    }


    @Operation(summary = "查询我的待检查任务", description = "查询我的待检查任务")
    @GetMapping(value = "listTodoTask")
    public R listTodoTask(Page<RiskHidCheckTaskEntity> page,RiskHidCheckTaskEntity riskHidCheckTaskEntity) {
        LambdaQueryWrapper<RiskHidCheckTaskEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(SecurityUtils.getUser()), RiskHidCheckTaskEntity::getCheckPerson, SecurityUtils.getUser().getId())
                .eq(RiskHidCheckTaskEntity::getTaskStatus, HidDicConstant.RISK_HID_CHECKLIST_TASK_STATUS_TODO)
                .like(com.hopes.boss.common.core.util.StringUtils.isNotBlank(riskHidCheckTaskEntity.getTaskName()),RiskHidCheckTaskEntity::getTaskName,riskHidCheckTaskEntity.getTaskName());
        return R.ok(riskHidCheckTaskService.page(page, wrapper).convert(RiskHidCheckTaskVO::new));
    }


    @Operation(summary = "查询我的已检查任务", description = "查询我的已检查任务")
    @GetMapping(value = "listCompletedTask")
    public R listCompletedTask(Page<RiskHidCheckTaskEntity> page,RiskHidCheckTaskEntity riskHidCheckTaskEntity) {
        LambdaQueryWrapper<RiskHidCheckTaskEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(SecurityUtils.getUser()), RiskHidCheckTaskEntity::getCheckPerson, SecurityUtils.getUser().getId())
                .eq(RiskHidCheckTaskEntity::getTaskStatus, HidDicConstant.RISK_HID_CHECKLIST_TASK_STATUS_COMPLETE)
                .like(com.hopes.boss.common.core.util.StringUtils.isNotBlank(riskHidCheckTaskEntity.getTaskName()),RiskHidCheckTaskEntity::getTaskName,riskHidCheckTaskEntity.getTaskName());
        return R.ok(riskHidCheckTaskService.page(page, wrapper).convert(RiskHidCheckTaskVO::new));
    }

    /**
     * 通过taskId查询隐患检查任务的检查项
     *
     * @param taskId taskId
     * @return R
     */
    @Operation(summary = "通过taskId查询隐患检查任务的检查项", description = "通过taskId查询隐患检查任务详情")
    @GetMapping("/hidCheckTaskRecord/{taskId}")
    public R listHidCheckTakRecordByTaskId(@PathVariable("taskId") Long taskId) {
        return R.ok(riskHidCheckTaskRecordService.listByTaskId(taskId));
    }


}
